package edu.ChildrensBooks.service

import grails.test.*
import org.springframework.validation.Errors
import org.springframework.validation.ObjectError
import edu.ChildrensBooks.model.Book
import edu.ChildrensBooks.service.BookService

class BookServiceIntegrationTests extends GrailsUnitTestCase {

	BookService bookService

    void testAdd() {
		boolean added = bookService.add(new Book("Title", "Author", null, null, 1, 2, null, "Math skills"))
		
		assertEquals added, true
		
		added = bookService.add(new Book("Title", "Author", null, null, 5, 3, "en", "Math skills"))
		
		assertEquals added, false
		
    }
	
	void testUpdateAndRandom() {
		bookService.add(new Book("Title", "Author", null, null, 1, 2, null, "Math skills"))
		
		Book randomBook = bookService.getRandomBook()
		Integer idValue = randomBook.getId()
		
		boolean updated = bookService.update(new Book("New Title", "New Author", null, null, 0, 1, null, "Math skills"), idValue)
		
		assertEquals updated, true
		
		updated = bookService.update(new Book(), idValue)
		
		assertEquals updated, false
	}
	
	void testRemove() {
		bookService.add(new Book("Title", "Author", null, null, 1, 2, null, "Math skills"))
		
		Book ensureBookIsThere = bookService.getBook("Author", "Title")
		assertNotNull ensureBookIsThere
		
		Book randomBook = bookService.getRandomBook()
		Integer idValue = randomBook.getId()
		
		boolean deleted = bookService.delete(idValue)
		
		Book ensureBookIsNotThere = bookService.getBook("Title", "Author")
		assertNull ensureBookIsNotThere
		
	}
}
